package com.gzhryc.shared_device.oem.h5.mng.controllers.cdcz;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZSlotInstantLog;
import com.gzhryc.shared_device.cdcz.code.services.CDCZSlotInstantLogService;
import com.gzhryc.shared_device.oem.h5.mng.dto.cdcz.ChargeSocketSlotInstantLogRecord;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChargeSocketSlotInstantLogController {

    static Logger log = Logger.getLogger(ChargeSocketSlotInstantLogController.class);

    @Route("/cdcz/use_logs/list.html")
    public String list(HttpServletRequest request) {
        return "/cdcz/use_logs.html";
    }


    @Route(value = { "/cdcz/use_logs/chart.data" })
    public AjaxResult chartData(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.NULL_DATA, "暂无数据");
        String day = WebUtils.getString(request, "day");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        Integer hh = WebUtils.getInteger(request, "hh");
        if (StringTools.isNotBlank(day) && StringTools.isNotBlank(deviceSn) && slotNum != null && hh != null) {
            try {
                int mm = 0, count = 0;
                Double power = 0.0, current = 0.0;
                Date startDate = DateUtils.parseDate(day + " " + String.format("%02d", hh) + ":00", "yyyy-MM-dd HH:mm");
                Date endDate = DateUtils.parseDate(day + " " + String.format("%02d", hh) + ":59", "yyyy-MM-dd HH:mm");
                List<CDCZSlotInstantLog> logList = CDCZSlotInstantLogService.self()
                        .findByKeysAndDate(deviceSn, slotNum, startDate, endDate);
                Map<String, CDCZSlotInstantLog> data = new HashMap<>();
                if (logList != null && logList.size() > 0) {
                    for (CDCZSlotInstantLog instantLog : logList) {
                        String time = DateFormatUtils.format(instantLog.getCreateDate(), "HH:mm");
                        data.put(time, instantLog);
                    }
                }
                //精确到分钟
                ChargeSocketSlotInstantLogRecord record = new ChargeSocketSlotInstantLogRecord();
                for (int i = 0; i < 180; i++) {
                    mm++;
                    if (mm == 60) {
                        hh++;
                        mm = 0;
                    }
                    String time = String.format("%02d:%02d", hh, mm);
                    record.getTimes().add(time);

                    CDCZSlotInstantLog instantLog = data.get(time);
                    if (instantLog != null) {
                        count = 0;
                        if (instantLog.getInstantPower() != null) {
                            record.getPowerData().add(instantLog.getInstantPower());
                            power = instantLog.getInstantPower();
                        } else {
                            record.getPowerData().add(0.0);
                            power = 0.0;
                        }
                        if (instantLog.getInstantCurrent() != null) {
                            record.getCurrentData().add(instantLog.getInstantCurrent());
                            current = instantLog.getInstantCurrent();
                        } else {
                            record.getCurrentData().add(0.0);
                            current = 0.0;
                        }
                    } else {
                        count++;
                        if (count > 3) {
                            record.getPowerData().add(0.0);
                            record.getCurrentData().add(0.0);
                        } else {
                            record.getPowerData().add(power);
                            record.getCurrentData().add(current);
                        }
                    }
                }

                result.setCode(DtoConstants.SUCCESS, null);
                result.setBody(record);
            } catch (ParseException e) {
                log.error(e.getMessage(), e);
            }
        }
        return result;
    }
}
